package com.fernandomdo.ServiceAdministrator;

import com.fernandomdo.ServiceAdministrator.Publisher.PublisherListItem;
import com.fernandomdo.ServiceAdministrator.Territory.TerritoryListItem;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class ViewActivity extends Activity implements ListTerritoryFragment.OnListItemSelectedListener, ListPublisherFragment.OnListItemSelectedListener {
	private static final String TAG = "ViewActivity";
	public static final int TERRITORY = 0;
	public static final int PUBLISHER = 1;
	
	private static ListFragment listView;
	private static Fragment infoView;
	private Bundle extras;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_view_layout);
		
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		Intent intent = getIntent();
		extras = intent.getExtras();
		
		TextView typeLabel = (TextView)findViewById(R.id.list_type_label);
		
		//TODO: use getResources().getConfiguration().orientation to determine how to display the fragments
		
		switch(extras.getInt("type")) {
		case TERRITORY: //Setup the fragments for territory viewing
			listView = new ListTerritoryFragment();
			infoView = new InfoTerritoryFragment();
			typeLabel.setText(getResources().getString(R.string.list_territories));			
			break;
		case PUBLISHER: //Setup fragments for publisher viewing
			listView = new ListPublisherFragment();
			infoView = new InfoPublisherFragment();
			typeLabel.setText(getResources().getString(R.string.list_publishers));
			break;
		default:
			break;
		}
		
		/*
		if(listView instanceof ListTerritoryFragment) {
			Log.d(TAG, "ListTerritoryFragment");
		}
		if(listView instanceof ListPublisherFragment) {
			Log.d(TAG, "ListTerritoryFragment");
		}
		*/
		
		fragmentTransaction.replace(R.id.list_fragment, listView, "listView");
		fragmentTransaction.replace(R.id.info_fragment, infoView, "infoView");
		fragmentTransaction.commit();
	}
	
	@Override
	public void onListItemSelected(Object item) {
		//Update the info fragment if it exists
		if(getFragmentManager().findFragmentById(R.id.info_fragment).isAdded()) { 
			if(item instanceof TerritoryListItem) {
				((InfoTerritoryFragment)infoView).updateInfo(item);				
			}
			if(item instanceof PublisherListItem) {
				((InfoPublisherFragment)infoView).updateInfo(item);
			}
			
			
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		switch(extras.getInt("type")) {
		case TERRITORY: //Setup the fragments for territory viewing
			getMenuInflater().inflate(R.menu.menu_territories, menu);			
			break;
		case PUBLISHER: //Setup fragments for publisher viewing
			getMenuInflater().inflate(R.menu.menu_publishers, menu);
			break;
		default:
			break;
		}		
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.add_territory:
			startActivity(new Intent(this, AddTerritory.class));
			return true;
		case R.id.add_publisher:
			startActivity(new Intent(this, AddPublisher.class));
			return true;
		default:
			break;		
		}	
		return super.onOptionsItemSelected(item);
	}
}